package web;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import service.GeradorArquivoService;
import entity.Cenario;
import entity.Estoria;
import entity.Regra;

@Controller
@Scope("view")
public class ManterEstoria extends BaseManagedBean implements Serializable {

	private Estoria estoria;

	private Regra regra;

	private Cenario cenario;

	private List<SelectItem> listaRegrasSelect;
	
	private String referencias;
	
	private Regra regraSelecionada;

	@Autowired
	private GeradorArquivoService gerador;

	@PostConstruct
	public void postConstruct() {

		estoria = new Estoria();

		estoria.setListaRegras(new ArrayList<Regra>());
		estoria.setListaCenarios(new ArrayList<Cenario>());

		listaRegrasSelect = new ArrayList<SelectItem>();

		criarRegra();
		criarCenario();
	}

	private void criarRegra() {
		regra = new Regra();
	}

	private void criarCenario() {
		cenario = new Cenario();
	}
	
	public void adicionarReferencia(){
		
	}

	public void gerarArquivo() throws IOException {

		byte[] arquivo = gerador.gerarArquivo(estoria);

		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();

		ec.responseReset(); // Some JSF component library or some Filter might
							// have set some headers in the buffer beforehand.
							// We want to get rid of them, else it may collide.
		ec.setResponseContentType("application/zip");

		ec.setResponseContentLength(arquivo.length); // Set it with the file
														// size. This header is
														// optional. It will
														// work if
														// it's omitted, but the
														// download progress
														// will be
														// unknown.

		ec.setResponseHeader("Content-Disposition", "attachment; filename=\""
				+ estoria.getNomeArquivo() + ".zip\""); // The Save As popup
														// magic is done here.
														// You
		// can give it any file name you want, this
		// only won't work in MSIE, it will use
		// current request URL as file name instead.

		OutputStream output = ec.getResponseOutputStream();
		// Now you can write the InputStream of the file to the above
		// OutputStream the usual way.
		// ...

		output.write(arquivo);

		fc.responseComplete(); // Important! Otherwise JSF will attempt to
								// render the response which obviously will fail
								// since it's already written with a file and
								// closed.
	}

	public void adicionarRegra() {

		final String codigoRegra = String.format("RN%02d", (estoria
				.getListaRegras().size() + 1));

		regra.setCodigo(codigoRegra);
		estoria.getListaRegras().add(regra);

		adicionarRegraNaListaSelect(regra);

		criarRegra();
	}

	private void adicionarRegraNaListaSelect(Regra regra) {

		listaRegrasSelect.add(new SelectItem(regra.getCodigo()));

	}

	public void adicionarCenario() {

		cenario.setCodigo(estoria.getListaCenarios().size() + 1);
		estoria.getListaCenarios().add(cenario);

		criarCenario();
	}

	public Estoria getEstoria() {
		return estoria;
	}

	public void setEstoria(Estoria estoria) {
		this.estoria = estoria;
	}

	public Regra getRegra() {
		return regra;
	}

	public void setRegra(Regra regra) {
		this.regra = regra;
	}

	public Cenario getCenario() {
		return cenario;
	}

	public void setCenario(Cenario cenario) {
		this.cenario = cenario;
	}

	public List<SelectItem> getListaRegrasSelect() {
		return listaRegrasSelect;
	}

	public void setListaRegrasSelect(List<SelectItem> listaRegrasSelect) {
		this.listaRegrasSelect = listaRegrasSelect;
	}

	public String getReferencias() {
		return referencias;
	}

	public void setReferencias(String referencias) {
		this.referencias = referencias;
	}

	public Regra getRegraSelecionada() {
		return regraSelecionada;
	}

	public void setRegraSelecionada(Regra regraSelecionada) {
		this.regraSelecionada = regraSelecionada;
	}

}
